package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.GoodsCommentService;
import com.baichang.taoshunda.service.GoodsService;
import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSGOODS)
public class BussinessGoodsMController extends BaseMobileController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCommentService goodsCommentService;

    /**
     * 查看商品列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findGoodsList")
    public void findGoodsList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer goodsTypeId = getInt(json, "goodsTypeId", 0);
                return goodsService.findGoodsList(bussId, goodsTypeId);
            }
        });
    }

    /**
     * 查看全部商品列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findAllGoodsList")
    public void findAllGoodsList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                return goodsService.findAllGoodsList(bussId, nowPage, 20);
            }
        });
    }

    /**
     * 添加商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addGoods")
    public void addGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                String name = getString(json, "name", "");
                Double price = getDouble(json, "price", 0D);
                Double oldPrice = getDouble(json, "oldPrice", 0D);//废弃不用
                Double bagging = getDouble(json, "bagging", 0D);
                String headPic = getString(json, "headPic", "");
                Integer typeId = getInt(json, "typeId", 0);
                Integer isFree = getInt(json, "isFree", 0);
                Integer isBespeak = getInt(json, "isBespeak", 0);
                Integer isHomeInstallation = getInt(json, "isHomeInstallation", 0);
                Integer isNoReasonReturn = getInt(json, "isNoReasonReturn", 0);
                Integer isInvoice = getInt(json, "isInvoice", 0);
                String images = getString(json, "images", "");
                String bannerImages = getString(json, "bannerImages", "");
                String goodsDetails = getString(json, "goodsDetails", "");
                String spec = getString(json, "spec", "[]");
                String unit = getString(json, "unit", "份");
                String commercialActivities = getString(json, "commercialActivities", "");
                String goodsVideo = getString(json, "goodsVideo", "");
                String goodsVideoImg = getString(json, "goodsVideoImg", "");
                return goodsService.addGoods(cityId, bussId, name, oldPrice, price, bagging, headPic, images, typeId, unit, spec, isFree, isInvoice, bannerImages, goodsDetails, isBespeak, isNoReasonReturn, isHomeInstallation, commercialActivities, goodsVideo, goodsVideoImg);
            }
        });
    }

    /**
     * 修改商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateGoods")
    public void updateGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);//商品编号
                Integer companyId = getInt(json, "bussId", 0);//商家编号
                String name = getString(json, "name", "");//名称
                Double price = getDouble(json, "price", 0D);//价格
                Double bagging = getDouble(json, "bagging", 0D);//包装费
                String headPic = getString(json, "headPic", "");//列表图片（去第一张图片）
                String images = getString(json, "images", "");//图片
                String spec = getString(json, "spec", "[]");//规格
                String unit = getString(json, "unit", "份");//单位
                Integer isBespeak = getInt(json, "isBespeak", 0);//是否可以预约
                Integer isHomeInstallation = getInt(json, "isHomeInstallation", 0);//是否上门服务   0否   1是
                Integer isNoReasonReturn = getInt(json, "isNoReasonReturn", 0);//无理由退款
                String goodsDetails = getString(json, "goodsDetails", "");//商品详情
                String commercialActivities = getString(json, "commercialActivities", "");//活动
                Integer isFree = getInt(json, "isFree", 0);//是否免费配送
                Integer isInvoice = getInt(json, "isInvoice", 0);//不传了   在商家设置是否支持发票
                String bannerImages = getString(json, "bannerImages", "");//背景图
                String goodsVideo = getString(json, "goodsVideo", "");
                String goodsVideoImg = getString(json, "goodsVideoImg", "");
                return goodsService.updateGoods(id, name, price, bagging, headPic, images, unit, spec, companyId, isBespeak, isHomeInstallation, isNoReasonReturn, goodsDetails, commercialActivities, isFree, isInvoice, bannerImages, goodsVideo, goodsVideoImg);
            }
        });
    }

    /**
     * 删除商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteGoods")
    public void deleteGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String id = getString(json, "id", "");
                return goodsService.deleteGoods(id);
            }
        });
    }

    /**
     * 商品上架
     *
     * @param request
     * @param response
     */
    @RequestMapping("/goodsUp")
    public void goodsUp(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                return goodsService.updateGoodsIsUpper(id, 1);
            }
        });
    }

    /**
     * 商品下架
     *
     * @param request
     * @param response
     */
    @RequestMapping("/goodsDown")
    public void goodsDown(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                return goodsService.updateGoodsIsUpper(id, 0);
            }
        });
    }

    /**
     * 修改商品排序
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateGoodsOrderNum")
    public void updateGoodsOrderNum(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String jsonData = getString(json, "jsonData", "[]");
                return goodsService.updateGoodsOrderNum(jsonData);
            }
        });
    }

    /**
     * 查看商品详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsDetails")
    public void getGoodsDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer goodsId = getInt(json, "goodsId", 0);
                return goodsService.getGoodsDetails(goodsId, 0, 0, 0);
            }
        });
    }

    /**
     * 商家的评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCommentByBussId")
    public void getCommentByBussId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int bussId = getInt(s, "goodsId", 0);
                int nowPage = getInt(s, "nowPage", 1);
                int pageSize = getInt(s, "pageSize", 20);
                return goodsCommentService.getCommentByBussId(bussId, nowPage, pageSize);
            }
        });
    }

    /**
     * 申请特价商品，调取支付接口
     */
    @RequestMapping("/goodsPay")
    public void goodsPay(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer goodsId = getInt(json, "goodsId", 0);
                Integer payType = getInt(json, "payType", 0);
                Integer busId = getInt(json, "busId", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(json, "price", 0));
                return goodsService.goodsPay(goodsId, busId, money, payType);
            }
        });
    }

    /**
     * 支付成功之后调用，修改商品为特价商品并创建缴费记录
     */
    //TODO  支付回调
    @RequestMapping("/addGoodsPayHistory")
    public void addGoodsPayHistory(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer goodsId = getInt(json, "goodsId", 0);
                Integer busId = getInt(json, "busId", 0);
                Integer payType = getInt(json, "payType", 0);
                Integer day = getInt(json, "day", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(json, "price", 0));
                BigDecimal goodsDiscountPrice = BigDecimal.valueOf(getDouble(json, "goodsDiscountPrice", 0));
                Integer discountNumber = getInt(json, "discountNumber", 0);
                return goodsService.addGoodsPayHistory(goodsId, busId, payType, money, day, goodsDiscountPrice, discountNumber);
            }
        });
    }

    /**
     * 再次推广的页面详情的数据
     */
    @RequestMapping("/discountDetail")
    public void discountDetail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer goodsId = getInt(json, "goodsId", 0);

                return goodsService.discountDetail(goodsId);
            }
        });
    }

}
